package com.ruoyi.mes.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.mes.vo.buy.BuyOrderVo;
import com.ruoyi.mes.vo.list.BuyNeedListVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.domain.BuyNeed;
import com.ruoyi.mes.service.IBuyNeedService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购需求Controller
 *
 * @author LongAndHao
 * @date 2023-04-04
 */
@Api(tags = "采购需求")
@RestController
@RequestMapping("/mes/need")
public class BuyNeedController extends BaseController {
    @Autowired
    private IBuyNeedService buyNeedService;

    /**
     * 查询采购需求列表
     */
    @ApiOperation(value = "查询采购需求列表")
    @PreAuthorize("@ss.hasPermi('mes:need:list')")
    @GetMapping("/list")
    public TableDataInfo list(BuyNeed buyNeed) {
        startPage();
        List<BuyNeed> list = buyNeedService.selectBuyNeedList(buyNeed);
        return getDataTable(list);
    }

    /**
     * 导出采购需求列表
     */
    @ApiOperation(value = "导出采购需求列表")
    @PreAuthorize("@ss.hasPermi('mes:need:export')")
    @Log(title = "采购需求", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BuyNeed buyNeed) {
        List<BuyNeed> list = buyNeedService.selectBuyNeedList(buyNeed);
        ExcelUtil<BuyNeed> util = new ExcelUtil<BuyNeed>(BuyNeed.class);
        util.exportExcel(response, list, "采购需求数据");
    }

    /**
     * 获取采购需求详细信息
     */
    @ApiOperation(value = "获取采购需求详细信息")
    @PreAuthorize("@ss.hasPermi('mes:need:query')")
    @GetMapping(value = "/{needId}")
    public AjaxResult getInfo(@PathVariable("needId") Long needId) {
        return success(buyNeedService.selectBuyNeedByNeedId(needId));
    }

    /**
     * 新增采购需求
     */
    @ApiOperation(value = "新增采购需求")
    @PreAuthorize("@ss.hasPermi('mes:need:add')")
    @Log(title = "采购需求", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BuyNeed buyNeed) {
        return toAjax(buyNeedService.insertBuyNeed(buyNeed));
    }

    /**
     * 修改采购需求
     */
    @ApiOperation(value = "修改采购需求")
    @PreAuthorize("@ss.hasPermi('mes:need:edit')")
    @Log(title = "采购需求", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BuyNeed buyNeed) {
        return toAjax(buyNeedService.updateBuyNeed(buyNeed));
    }

    /**
     * 删除采购需求
     */
    @ApiOperation(value = "删除采购需求")
    @PreAuthorize("@ss.hasPermi('mes:need:remove')")
    @Log(title = "采购需求", businessType = BusinessType.DELETE)
    @DeleteMapping("/{needIds}")
    public AjaxResult remove(@PathVariable Long[] needIds) {
        return toAjax(buyNeedService.deleteBuyNeedByNeedIds(needIds));
    }

    //*******************************************

    /**
     * 查 - 采购需求分页 - 采购需求单号
     *
     * @param buyNeed
     * @return
     */
    @GetMapping("/buyNeedList")
    public TableDataInfo buyNeedList(BuyNeed buyNeed) {
        startPage();
        List<BuyNeedListVo> list = buyNeedService.buyNeedList(buyNeed);
        return getDataTable(list);
    }

    /**
     * 查 - 采购需求详情
     *
     * @param id 采购需求id
     * @return
     */
    @GetMapping("/getDetais")
    public AjaxResult getDetais(@RequestParam Long id) {
        return success(buyNeedService.getDetais(id));
    }

    /**
     * 删 - 采购需求
     *
     * @param id 采购需求id
     * @return
     */
    @DeleteMapping("/deleteBuyNeed")
    public AjaxResult deleteBuyNeed(@RequestParam Long id) {
        return buyNeedService.deleteBuyNeed(id);
    }

    /**
     * 生成采购订单
     *
     * @param buyOrderVo
     * @return
     */
    @PostMapping("/createBuyorder")
    public AjaxResult createBuyorder(@RequestBody BuyOrderVo buyOrderVo) {
        return buyNeedService.createBuyorder(buyOrderVo);
    }


}
